package com.example.studentmanagement;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.studentmanagement.dao.CurriculumDao;
import com.example.studentmanagement.dao.MycourseDao;
import com.example.studentmanagement.entity.Curriculum;
import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;
import com.example.studentmanagement.view.Bottom_Bar;
import com.example.studentmanagement.view.Top_Bar;

import java.util.ArrayList;
import java.util.List;


public class CurriculumActivity extends AppCompatActivity {
    private Top_Bar top_bar ;
    private Bottom_Bar bottom_bar ;
    private CurriculumDao curriculumdao ;
    private MycourseDao mycoursedao ;
    private CurriculumAdapter adapter ;
    private List<String[]> curriculumInfoList ;
    private ListView listView ;
    private ArrayList<Curriculum> curriculumList ;
    private ArrayAdapter<Curriculum> adapter1 ;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studentlist);

        DBUtil dbUtil = new DBUtil(CurriculumActivity.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        DBUtil.db=db;


        top_bar = findViewById(R.id.studentlist_top);
        bottom_bar = findViewById(R.id.studentlist_bottom);
        top_bar.setLeftBtnNoVisible(true);//不显示
        top_bar.setRightBtnNoVisible(true);
        top_bar.setTitleText("课程");
        bottom_bar.setCourseBtnBackground(R.color.q2);

        listView = findViewById(R.id.studentlistView);

        //拿到登录界面的用户Id
        SharedPreferences sharedPreferences = getSharedPreferences("loginId" , Context.MODE_PRIVATE);
        String studentId = sharedPreferences.getString("s_id", "");

        curriculumdao = new CurriculumDao();
        mycoursedao = new MycourseDao();
        List<String> curriculumIds = mycoursedao.getChosenCurriculumIds(studentId);

//        adapter = new ArrayAdapter<com.example.studentmanagement.entity.Curriculum>(this ,
//                R.layout.curriculumlist , R.id.curriculum_name,curriculumIds){
//            @Override
//            public View getView(int position , View converView , ViewGroup parent){
//                View view = super.getView(position , converView , parent);
//                TextView textName = view.findViewById(R.id.student_id);
//                TextView textId = view.findViewById(R.id.student_name);
//                Curriculum curriculum =  getItem(position);
//
//                if (curriculum != null) {
//
//                    textName.setText(String.valueOf(curriculum.getcName()));
//                    textId.setText(String.valueOf(curriculum.getcId()));
//                }
//
//                return view ;
//            }
//
//        };


        Log.d("", "--------------------------------"+curriculumIds+"-------------------------------");
        curriculumInfoList = new ArrayList<>();
        for (String curriculumId : curriculumIds) {
            String curriculumName = curriculumdao.getCourseName(curriculumId);
            Log.d("", "--------------------------------"+curriculumName+"-------------------------------");
            curriculumInfoList.add(new String[]{curriculumId, curriculumName});
        }

        adapter = new CurriculumAdapter(this, curriculumInfoList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView getId = view.findViewById(R.id.curriculum_name);
                Log.d("", "+++++++++++++++++++++"+getId+"++++++++++++++++");
                String curriculumId = getId.getText().toString() ;
                Curriculum curriculum = curriculumdao.getCourseDetails(curriculumId);

                // 显示课程详细信息的对话框
                showCurriculumDetailsDialog(curriculum);
            }
        });

        bottom_bar.setStudentBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CurriculumActivity.this , StudentListActivity.class) ;
                startActivity(intent);
                finish();
            }
        });
        bottom_bar.setPersonBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CurriculumActivity.this , PersonalActivity.class) ;
                startActivity(intent);
                finish();
            }
        });

    }

    private void showCurriculumDetailsDialog(Curriculum curriculum) {
        if (curriculum != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(CurriculumActivity.this);
            builder.setTitle("课程信息");
            builder.setMessage("课程ID: " + curriculum.getcName() + "\n"
                    + "课程名称: " + curriculum.getcId() + "\n"
                    + "授课教师: " + curriculum.getcTeacher() + "\n"
                    + "上课班级: " + curriculum.getcClass() + "\n"
                    + "上课时间: " + curriculum.getcTime()+"学时");
            builder.setPositiveButton("确定", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }}
}


